package com.java.nio.zerocopy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author jl
 * Created on 2020/9/28
 */
public class NewIOServer {

    public static void main(String[] args) throws Exception {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress("127.0.0.1", 7000));
        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
        int read = 0;
        // 循环监听
        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            // 循环读取
            while (read != -1){
                try {
                    read = socketChannel.read(byteBuffer);
                } catch (IOException e) {
                    break;
                }
                byteBuffer.rewind(); // 倒带 position=0 mark作废
            }
        }
    }
}
